package spoon.sbobet.service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import spoon.domain.Betting;
import spoon.sbobet.domain.GameLink;

@Service
@Qualifier("sbobetFootballService")
public class SbobetFootballService extends SbobetServiceImpl {

	@Value("${sbobet.football.url}")
	private String connectionUrl;

	@Value("${sbobet.football.item}")
	private String item;

	@Value("${sbobet.football.name}")
	private String itemName;

	@Override
	public List<GameLink> getLink() {

		log.info("===================================================================");
		log.info("    Sbobet {} 링크 정보 검색 시작", itemName);

		List<GameLink> linkList = new ArrayList<GameLink>();

		int gameCnt = 0;

		Elements elements = httpService.getElement(connectionUrl, "script");
		dynamicScript = httpService.getJavascript("https://www.sbobet.com" + elements.select("[src^=/en/resource/e/euro-dynamic]").attr("src"));
		addDynamicData();
		String link = elements.last().toString();

		Pattern p = Pattern.compile(linkPattern);
		Matcher m = p.matcher(link);

		while (m.find()) {
			GameLink gameLink = new GameLink();
			gameLink.setItem(item);
			gameLink.setItemName(itemName);
			getLinkInfo(gameLink, m.group());
			linkList.add(gameLink);
			gameCnt++;
		}

		log.info("    Sbobet {} 링크 정보 검색 종료. 전체 추가 게임수 = {}", itemName, gameCnt);
		log.info("===================================================================");

		return linkList;

	}

	@Override
	@Async
	public void getGame(GameLink gameLink) {

		long startTime = System.currentTimeMillis();

		// 게임별 상세 베팅 가져오기
		this.getBetting(gameLink);
		
		// 데이터 입력
		int bettingCnt = addBetting(gameLink);
		
		log.info("   사이트: {}, 종목: {}, 국가: {}, 리그: {}, 팀명: {} vs {}, 베팅수: {}, 소요시간: {}초", gameLink.getSite(), gameLink.getItem(), gameLink.getNational(),
		gameLink.getLeague(), gameLink.getHomeTeam(), gameLink.getAwayTeam(), bettingCnt, (System.currentTimeMillis() - startTime) / 1000.0f);
	}

	protected void getBetting(GameLink gameLink) {

		Elements elements = httpService.getElement(gameLink.getLink(), "script");

		String data = elements.last().toString();

		Pattern p = Pattern.compile(dataPattern);
		Matcher m = p.matcher(data);

		System.out.println("--------------------------------------------------------");
		System.out.println(gameLink.getLink());
		while (m.find()) {
			System.out.println(m.group());
			getBetting(gameLink, m.group().replace("[", "").replace("]", ""));
		}
	}

	protected void getBetting(GameLink gameLink, String data) {
		String[] datas = data.split(",");

		int mainCode = Integer.parseInt(datas[1]);
		int subCode = Integer.parseInt(datas[3]);
		Double handi = Double.parseDouble(datas[6]);
		Betting betting;

		switch (mainCode) {
		// Asian Handicap
		case 1:
			betting = new Betting();
			betting.setBettingType("1-2");
			betting.setBettingSet("Full Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("Handicap");
			betting.setBettingName("Asian Handicap");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr(getHandi(handi));
			betting.setAwayRate(Double.parseDouble(datas[9]));
			betting.setAwayDescr(getHandi(handi * -1));
			gameLink.getBettingList().add(betting);
			break;
		// Over Under
		case 3:
			betting = new Betting();
			betting.setBettingType("1-2");
			betting.setBettingSet("Full Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("Over/Under");
			betting.setBettingName("Over Under");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr(handi + " Over");
			betting.setAwayRate(Double.parseDouble(datas[9]));
			betting.setAwayDescr(handi + " Under");
			gameLink.getBettingList().add(betting);
			break;
		// 1X2
		case 5:
			betting = new Betting();
			betting.setBettingType("1X2");
			betting.setBettingSet("Full Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("1X2");
			betting.setBettingName("1X2");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr(gameLink.getHomeTeam());
			betting.setDrawRate(Double.parseDouble(datas[9]));
			betting.setDrawDescr("Draw");
			betting.setAwayRate(Double.parseDouble(datas[10]));
			betting.setAwayDescr(gameLink.getAwayTeam());
			gameLink.getBettingList().add(betting);
			break;
		// First Half Asian Handicap
		case 7:
			betting = new Betting();
			betting.setBettingType("1-2");
			betting.setBettingSet("Half Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("Handicap");
			betting.setBettingName("First Half Asian Handicap");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr(getHandi(handi));
			betting.setAwayRate(Double.parseDouble(datas[9]));
			betting.setAwayDescr(getHandi(handi * -1));
			gameLink.getBettingList().add(betting);
			break;
		// First Half 1X2
		case 8:
			betting = new Betting();
			betting.setBettingType("1X2");
			betting.setBettingSet("Half Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("1X2");
			betting.setBettingName("First Half 1X2");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr(gameLink.getHomeTeam());
			betting.setDrawRate(Double.parseDouble(datas[9]));
			betting.setDrawDescr("Draw");
			betting.setAwayRate(Double.parseDouble(datas[10]));
			betting.setAwayDescr(gameLink.getAwayTeam());
			gameLink.getBettingList().add(betting);
			break;
		// First Half Over Under
		case 9:
			betting = new Betting();
			betting.setBettingType("1-2");
			betting.setBettingSet("Half Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("Over/Under");
			betting.setBettingName("Over Under");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr(handi + " Over");
			betting.setAwayRate(Double.parseDouble(datas[9]));
			betting.setHomeDescr(handi + " Under");
			gameLink.getBettingList().add(betting);
			break;
		// Money Line
		case 11:
			System.out.println("Money Line");
			System.out.println(data);
			break;
		// Double Chance
		case 15:
			betting = new Betting();
			betting.setBettingType("1X2");
			betting.setBettingSet("Full Time");
			betting.setBettingScore("Goal");
			betting.setBettingCode("Double Chance");
			betting.setBettingName("Double Chance");
			betting.setHandicap(handi);
			betting.setHomeRate(Double.parseDouble(datas[8]));
			betting.setHomeDescr("1X");
			betting.setDrawRate(Double.parseDouble(datas[9]));
			betting.setDrawDescr("12");
			betting.setAwayRate(Double.parseDouble(datas[10]));
			betting.setAwayDescr("2X");
			gameLink.getBettingList().add(betting);
			break;
		}
		
		if (mainCode > 99) {
			
		}

	}

}
